//Documentation complete
/**
 * @author Jeff
 * @version 2009AP05
 */

package control;

import problemDomain.*;
import broker.*;
import static problemDomain.ErrorBean.ErrorCode.*;

import java.util.List;

/**
 * Control object for menu object operations.
 */
public class MenuControl extends Control {
	
	/* Attributes */
	
	private MenuBroker mb;
	private MenuItemBroker mib;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs a <code>MenuControl</code>.
	 * @throws ErrorBean if the database is not available
	 */
	public MenuControl () throws ErrorBean {
		mb = MenuBroker.getBroker();
		mib = MenuItemBroker.getBroker();
	}
	
	/* END Constructors */
	
	
	/* Methods */
	
	/**
	 * Creates a new menu.
	 * @param name the name of the menu
	 * @param description the description for the menu
	 * @param active the active status of the menu
	 * @throws ErrorBean if the input data are not correct
	 */
	public void create (String name, String description, String active) throws ErrorBean {
		boolean b_active = Boolean.parseBoolean(active);
		MenuBean menu = new MenuBean(0,name,description, null, b_active);
		mb.persist(menu);
		
	}
	
	/**
	 * Searches for menus which match the search criteria.
	 * @param id the ID to search for (0 excludes this field when searching)
	 * @param name the name to search for (<code>""</code> excludes this field when searching)
	 * @param active the active status to search for (<code>null</code> excludes this field when searching)
	 * @return a <code>List</code> of results
	 * @throws ErrorBean if the input data are not correct
	 */
	public List<MenuBean> search (String id, String name, String active) throws ErrorBean {
		MenuBean menu = new MenuBean();
		
		if (id != null && !id.equals(""))
			menu.setId(checkMenuId(id));
		
		if (name != null && !"".equals(name))
			menu.setName(name);
		
		menu.setActive(checkActive(active));
		
		return mb.search(menu);
	}
	
	/**
	 * Updates a menu.
	 * @param id the ID of the menu
	 * @param name the new name of the menu
	 * @param description the new description for the menu
	 * @param active the active status of the menu
	 * @throws ErrorBean if the input data are not correct
	 */
	public void update (String id, String name, String description, String active) throws ErrorBean {
		MenuBean menu = new MenuBean(checkMenuId(id), name, description, null, checkActive(active));
		mb.persist(menu);
	}
	
	/**
	 * Deletes a menu.
	 * @param id the ID of the menu
	 * @throws ErrorBean if the input data are not correct
	 */
	public void delete (String id) throws ErrorBean {
		MenuBean menu = new MenuBean(checkMenuId(id));
		mb.remove(menu);
	}
	
	/**
	 * Lists all the menus. 
	 * @return a <code>List</code> of the results
	 * @throws ErrorBean if there is an error retrieving the data
	 */
	public List<MenuBean> listAll() throws ErrorBean {
		return mb.listAll();
	}
	
	/**
	 * Lists all items in the specified menu.
	 * @param menuId the ID of the menu
	 * @return the <code>List</code> of results
	 * @throws ErrorBean if the input data are not correct
	 */
	public List<MenuItemBean> listItemInMenu(String menuId) throws ErrorBean {
		MenuBean menu = new MenuBean(checkMenuId(menuId));
		MenuItemBean searchBean = new MenuItemBean();
		searchBean.setMenu(menu);
		return mib.search(searchBean);
	}
	
	/**
	 * Lists all menu items.
	 * @return a <code>List</code> of results
	 * @throws ErrorBean if there is an error retrieving the data
	 */
	public List<MenuItemBean> listMenuItem() throws ErrorBean {
		return mib.listAll();
	}
	
	/**
	 * Adds an item to a menu.
	 * @param menuId the ID of the menu
	 * @param menuItemID the ID of the item
	 * @throws ErrorBean if the input data are not correct
	 */
	public void addItem (String menuId, String menuItemID) throws ErrorBean {
		// search for the menu item and menu, and change the menu where the menu item belongs to
		MenuItemBean menuItem = new MenuItemBean(checkMenuItemId(menuItemID));
		MenuBean menu = new MenuBean(checkMenuId(menuId));
		List<MenuItemBean> list = mib.search(menuItem);
		List<MenuBean> menuList = mb.search(menu);
		menuItem = list.get(0);
		menuItem.setMenu(menuList.get(0));
		mib.persist(menuItem);
	}
	
	/**
	 * Removes an item from a menu.
	 * @param menuItemID the ID of the menu item to remove
	 * @throws ErrorBean if the input data are not correct
	 */
	public void removeItem (String menuItemID) throws ErrorBean {
		// search for the menu item, and set its reference to menu to null
		MenuItemBean menuItem = new MenuItemBean(checkMenuItemId(menuItemID));
		List<MenuItemBean> list = mib.search(menuItem);
		menuItem = list.get(0);
		menuItem.setMenu(null);
		mib.persist(menuItem);
	}
	
	/* END Methods */
	
	
	/* Data Checks */
	
	private int checkMenuId (String id) throws ErrorBean {
		return checkInt(id, MENU_ID_INVALID, this);}
	
	private int checkMenuItemId (String id) throws ErrorBean {
		return checkInt(id, MENUITEM_ID_INVALID, this);}
	
	private Boolean checkActive (String active) throws ErrorBean {
		return checkBoolean(active);
	}
	/* END Data Checks */
	
}
